<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class InfoController extends Controller
{
    //
    public function index()
    {
        // dd(session("homeuser"));
    	return view("home.info");
    }
    public function xg()
    {
        $id = session("homeuser")->userid;
        $data = DB::table("user")->where("userid",$id)->first();
    	return view("home.xg",["data"=>$data]);
    }
    public function update(Request $request)
    {
        //dd($request);
    	$data = $request->only("sex","age","phone","email");
    	$messages = [
    		"required" => ":attribute 的字段是必要的",
    	];
    	$this->validate($request, [
            "userid" => "unique:user",
	        'age' => 'required | numeric |max:1000',
	        'sex' => 'required',
            'email' => 'required|email',
	        'phone' => 'required | numeric',
    	],$messages);
        $m = DB::table("user")->where("userid",session("homeuser")->userid)->update($data);
        // session(["homeuser"=>$data]);
        if($m > 0){
            return redirect("home/info");
        }
    }
    public function grtx(Request $request)
    {
        // $a = $request->file('grtx');
        if($request->hasFile("grtx")){  
            $file = $request->file("grtx");
            $ext = $file->getClientOriginalExtension();            
            $picname = time().rand(1000,9999).".".$ext;            
            $file->move("./uploads/",$picname);
            $id = session("homeuser")->userid;
            DB::table("user")->where("userid",$id)->update(["photo"=>$picname]);
            return redirect("home/xg");
        }
    }
}
